using System;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Globalization;
using AjaxControlToolkit;
using System.Text.RegularExpressions;

[assembly: System.Web.UI.WebResource("AjaxControlToolkitEx.PopupPanelExtenderBehavior.js", "text/javascript")]

namespace AjaxControlToolkitEx
{
    [RequiredScript(typeof(CommonToolkitScripts))]
    [Designer(typeof(PopupPanelExtenderDesigner))]
    //[ClientScriptResource("AjaxControlToolkitEx.PopupPanelExtenderBehavior", "AjaxControlToolkitEx.PopupPanelExtenderBehavior.js")]
    [ClientScriptResource("AjaxControlToolkitEx.PopupPanelExtenderBehavior", "AjaxControlToolkitEx.PopupPanelExtenderBehavior")]
    [TargetControlType(typeof(Control))]
    public class PopupPanelExtender : ExtenderControlBaseEx
    {
        // TODO: Add your property accessors here.
        //

        [ExtenderControlProperty]
        [DefaultValue("")]
        [IDReferenceProperty(typeof(WebControl))]
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1706:ShortAcronymsShouldBeUppercase", Justification = "Following ASP.NET AJAX pattern")]
        public string PopupButtonID
        {
            get { return GetPropertyValue("PopupButtonID", ""); }
            set { SetPropertyValue("PopupButtonID", value); }
        }

        [ExtenderControlProperty]
        [DefaultValue("")]
        [IDReferenceProperty(typeof(WebControl))]
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1706:ShortAcronymsShouldBeUppercase", Justification = "Following ASP.NET AJAX pattern")]
        public string HangonControlID
        {
            get { return GetPropertyValue("HangonControlID", ""); }
            set { SetPropertyValue("HangonControlID", value); }
        }

        [ExtenderControlProperty]
        [DefaultValue("")]
        [IDReferenceProperty(typeof(WebControl))]
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1706:ShortAcronymsShouldBeUppercase", Justification = "Following ASP.NET AJAX pattern")]
        public string CloseButtonID
        {
            get { return GetPropertyValue("CloseButtonID", ""); }
            set { SetPropertyValue("CloseButtonID", value); }
        }

        [ExtenderControlProperty]
        [DefaultValue("")]
        [IDReferenceProperty(typeof(WebControl))]
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1706:ShortAcronymsShouldBeUppercase", Justification = "Following ASP.NET AJAX pattern")]
        public string OnCloseFocusControlID
        {
            get { return GetPropertyValue("OnCloseFocusControlID", ""); }
            set { SetPropertyValue("OnCloseFocusControlID", value); }
        }

        [ExtenderControlProperty]
        [DefaultValue("")]
        [IDReferenceProperty(typeof(WebControl))]
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1706:ShortAcronymsShouldBeUppercase", Justification = "Following ASP.NET AJAX pattern")]
        public string DragHandleID
        {
            get {
                string dragHandleID = GetPropertyValue("DragHandleID", "");
                if (string.IsNullOrEmpty(dragHandleID))
                {
                    dragHandleID = TargetControlID;
                }
                return dragHandleID;
            }

            set { SetPropertyValue("DragHandleID", value); }
        }

        [ExtenderControlProperty]
        [DefaultValue(true)]
        public bool GrayOutBackground
        {
            get { return GetPropertyValue("GrayOutBackground", true); }
            set { SetPropertyValue("GrayOutBackground", value); }
        }

        [ExtenderControlProperty]
        [DefaultValue(0)]
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1706:ShortAcronymsShouldBeUppercase", Justification = "Following ASP.NET AJAX pattern")]
        public int VerticalOffset
        {
            get { return GetPropertyValue("VerticalOffset", 0); }
            set { SetPropertyValue("VerticalOffset", value); }
        }

        [DefaultValue("")]
        [ExtenderControlProperty]
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1706:ShortAcronymsShouldBeUppercase", Justification = "Following ASP.NET AJAX pattern")]
        public string HiddenFieldID
        {
            get { return GetPropertyValue("HiddenFieldID", ""); }
            set { SetPropertyValue("HiddenFieldID", value); }
        }

        [DefaultValue("")]
        [ExtenderControlProperty]
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1706:ShortAcronymsShouldBeUppercase", Justification = "Following ASP.NET AJAX pattern")]
        public string HiddenFieldOldID
        {
            get { return GetPropertyValue("HiddenFieldOldID", ""); }
            set { SetPropertyValue("HiddenFieldOldID", value); }
        }

        [DefaultValue("")]
        [ExtenderControlProperty]
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1706:ShortAcronymsShouldBeUppercase", Justification = "Following ASP.NET AJAX pattern")]
        public string HiddenFieldPosID
        {
            get { return GetPropertyValue("HiddenFieldPosID", ""); }
            set { SetPropertyValue("HiddenFieldPosID", value); }
        }
        

        protected string HFID
        {
            get { return ID + "hf"; }
        }
        protected string HFOldID
        {
            get { return ID + "hfold"; }
        }
        protected string HFPosID
        {
            get { return ID + "hfpos"; }
        }

        protected override void OnInit(EventArgs e)
        {
            CreateClientHiddenField();
            Page.PreLoad += new EventHandler(Page_PreLoad);
            base.OnInit(e);
        }

        void Page_PreLoad(object sender, EventArgs e)
        {
            if (((Page)sender).IsPostBack)
            {
                LoadClientHiddenFieldValue();
            }
        }
        
        private void CreateClientHiddenField()
        {
            ExtenderControlBaseEx.HiddenFieldEx field = new ExtenderControlBaseEx.HiddenFieldEx();
            field.ID = HFID;
            Controls.Add(field);
            HiddenFieldID = GetClientID(HFID);

            ExtenderControlBaseEx.HiddenFieldEx field3 = new ExtenderControlBaseEx.HiddenFieldEx();
            field3.ID = HFOldID;
            Controls.Add(field3);
            HiddenFieldOldID = GetClientID(HFOldID);


            ExtenderControlBaseEx.HiddenFieldEx field2 = new ExtenderControlBaseEx.HiddenFieldEx();
            field2.ID = HFPosID;
            Controls.Add(field2);
            HiddenFieldPosID = GetClientID(HFPosID);
        }
        private string Value = string.Empty;
        private string ValueOld = string.Empty;
        private string ValuePos = string.Empty;
        private void LoadClientHiddenFieldValue()
        {
            ExtenderControlBaseEx.HiddenFieldEx hiddenField = (ExtenderControlBaseEx.HiddenFieldEx)NamingContainer.FindControl(HFID);

            if ((hiddenField != null) && !string.IsNullOrEmpty(hiddenField.Value))
            {
                Value = hiddenField.Value;
            }

            ExtenderControlBaseEx.HiddenFieldEx hiddenField3 = (ExtenderControlBaseEx.HiddenFieldEx)NamingContainer.FindControl(HFOldID);

            if ((hiddenField3 != null) && !string.IsNullOrEmpty(hiddenField3.Value))
            {
                ValueOld = hiddenField3.Value;
            }


            ExtenderControlBaseEx.HiddenFieldEx hiddenField2 = (ExtenderControlBaseEx.HiddenFieldEx)NamingContainer.FindControl(HFPosID);

            if ((hiddenField2 != null) && !string.IsNullOrEmpty(hiddenField2.Value))
            {
                ValuePos = hiddenField2.Value;
            }
        }
        protected void SaveClientClientHiddenFieldValue()
        {
            ExtenderControlBaseEx.HiddenFieldEx hiddenField = (ExtenderControlBaseEx.HiddenFieldEx)NamingContainer.FindControl(HFID);

            if (hiddenField != null)
            {
                hiddenField.Value = Value;
            }

            ExtenderControlBaseEx.HiddenFieldEx hiddenField3 = (ExtenderControlBaseEx.HiddenFieldEx)NamingContainer.FindControl(HFOldID);

            if (hiddenField3 != null)
            {
                hiddenField3.Value = ValueOld;
            }


            ExtenderControlBaseEx.HiddenFieldEx hiddenField2 = (ExtenderControlBaseEx.HiddenFieldEx)NamingContainer.FindControl(HFPosID);

            if (hiddenField2 != null)
            {
                hiddenField2.Value = ValuePos;
            }
        }
        protected override void CreateChildControls()
        {
            base.CreateChildControls();
            this.BehaviorID = GetClientID(ID) + "_behaviour";
        }
        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);

            SaveClientClientHiddenFieldValue();

            if (_operation != null)
            {
                string operation = _operation;
                if (ScriptManager.GetCurrent(Page).IsInAsyncPostBack)
                {
                    ScriptManager.GetCurrent(Page).RegisterDataItem(TargetControl, operation);
                }
                else
                {
                    string script = string.Format(CultureInfo.InvariantCulture, @"
                    (function() {{
                        var fn = function() {{
                            var behavior = $find('{0}');
                            if (behavior) {{
                                if ('{1}' == 'show'){{
                                    behavior.hide();
                                    behavior.show();
                                }}else
                                    behavior.hide();
                            }}
                            Sys.Application.remove_load(fn);
                        }};
                        Sys.Application.add_load(fn);
                    }})();", BehaviorID, operation);
                    ScriptManager.RegisterStartupScript(this, typeof(PopupPanelExtender), operation + BehaviorID, Helper.Trim(script), true);
                }
            }
            if (_closechildren != null)
            {
                string script = string.Format(CultureInfo.InvariantCulture, @"
                (function() {{
                    var fn = function() {{
                        var behavior = $find('{0}');
                        if (behavior) {{
                            behavior.closechildren();
                        }}
                        Sys.Application.remove_load(fn);
                    }};
                    Sys.Application.add_load(fn);
                }})();", BehaviorID);
                ScriptManager.RegisterStartupScript(this, typeof(DropDownPanelExtender), "CloseChildren" + BehaviorID, Helper.Trim(script), true);
            }
            if (_closebtnClientID != null)
            {
                string script = string.Format(CultureInfo.InvariantCulture, @"
                (function() {{
                    var fn = function() {{
                        var behavior = $find('{0}');
                        if (behavior) {{
                            behavior.set_CloseButtonID('{1}');
                        }}
                        Sys.Application.remove_load(fn);
                    }};
                    Sys.Application.add_load(fn);
                }})();", BehaviorID, _closebtnClientID);
                ScriptManager.RegisterStartupScript(this, typeof(DropDownPanelExtender), "SetCloseButtonID" + BehaviorID, Helper.Trim(script), true);
            }
            if (_hangControlClientID != null)
            {
                string script = string.Format(CultureInfo.InvariantCulture, @"
                (function() {{
                    var fn = function() {{
                        var behavior = $find('{0}');
                        if (behavior) {{
                            behavior.set_HangonControlID('{1}');
                        }}
                        Sys.Application.remove_load(fn);
                    }};
                    Sys.Application.add_load(fn);
                }})();", BehaviorID, _hangControlClientID);
                ScriptManager.RegisterStartupScript(this, typeof(DropDownPanelExtender), "SetHangControlID" + BehaviorID, Helper.Trim(script), true);
            }

        }

        public bool PanelVisible
        {
            get
            {
                return (Value == "1");
            }
        }

        private string _operation = null;

        public void Show()
        {
            string operation = "show";
            _operation = operation;
            Value = "1";
        }

        public void Hide()
        {
            string operation = "hide";
            _operation = operation;
            Value = "0";
        }

        private string _closechildren = null;
        public void CloseChildren()
        {
            _closechildren = "closechildren";
        }

        private string _closebtnClientID = null;
        public void SetCloseButtonAtRuntime(string closebtnClientID)
        {
            _closebtnClientID = closebtnClientID;
        }
        private string _hangControlClientID = null;
        public void SetHangonControlAtRuntime(string hangControlClientID)
        {
            _hangControlClientID = hangControlClientID;
        }
    }
}
